import type { locationType } from '@/types/component'
import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useLocationStore = defineStore(
  'location',
  () => {
    /**
     * * 经纬度信息
     */
    const location = ref<locationType>()

    /**
     * * 保存经纬度信息，获取设备列表使用
     * @param val  传入经纬度
     */
    const setLocation = (val: locationType) => {
      location.value = { ...location.value, ...val }
    }

    /**
     * * 清理经纬度信息，退出时使用
     */
    const clearLocation = () => {
      location.value = undefined
      uni.removeStorageSync('location')
    }

    return {
      // 定位信息
      location,
      setLocation,
      clearLocation,
    }
  },

  {
    // 数据持久化
    persist: {
      storage: {
        setItem(key, value) {
          uni.setStorageSync(key, value)
        },
        getItem(key) {
          return uni.getStorageSync(key)
        },
      },
    },
  },
)
